<template>
  <confirm-dialog
    :title="title"
    :visible="visible"
    :loading="loading"
    @handleClose="handleClose"
    width="25%"
  >
  <template slot="content">
    <p>{{content}}</p>
    <ul v-if="Array.isArray(data)">
      <li v-for="(item, index) in data" :key="index">{{item.name}}</li>
    </ul>
    <ul v-else>
      <li>{{data.name}}</li>
    </ul>
  </template>
  <template slot="footer">
    <el-button @click="handleClose">{{$t('common.cancel')}}</el-button>
    <el-button :disabled="loading" type="primary" @click="submit">{{$t('common.confirm')}}</el-button>
  </template>
  </confirm-dialog>
</template>
<script lang="ts">
import Vue from 'vue'
import FormDialop from '../../../components/Dialog/formDialog.vue';
import ConfirmDialog from '../../../components/Dialog/confirmDialog.vue';
export default Vue.extend({
  components: {
    FormDialop,
    ConfirmDialog
  },
  props: {
    submitMethod: {
      type: Function,
      default() {
        return (formData: any) => Promise.resolve();
      }
    }
  },
  data() {
    return {
      title: '',
      visible: false,
      loading: false,
      content: '',
      data: {},
      action: ''
    }
  },
  methods: {
    handleClose () {
      this.visible = false;
      this.loading = false;
    },
    init (obj: any) {
      this.visible = true;
      this.data = obj.data as any;
      this.action = obj.type;
      switch (obj.type) {
        case 'REBOOT':
          this.title = this.$t('desktopManage.operation.restart').toString();
          break;
        case 'POWERON':
          this.title = this.$t('desktopManage.operation.boot').toString();
          break;
        case 'SHUTDOWN':
          this.title = this.$t('desktopManage.operation.shutdown').toString();
          break;
        case 'RESET':
          this.title = this.$t('desktopManage.operation.reset').toString();
          break;
        case 'SUSPEND':
          this.title = this.$t('desktopManage.operation.logout').toString();
          break;
        case 'POWEROFF':
          this.title = this.$t('desktopManage.operation.powerOff').toString();
          break;
        case 'DISCONNECT':
          this.title = this.$t('desktopManage.operation.disconnect').toString();
          break;
        case 'UnassignUser':
          this.title = this.$t('desktopManage.operation.unassignUser') + ': ' + obj.data.occupied_user.database.account_name || obj.data.occupied_user.vmware.occupied_username;
      }
      if (obj.data.length > 1) {
        this.content = this.$t('desktopManage.message.batchOperation', { operation: this.title }).toString();
      } else {
        if (obj.type == 'UnassignUser') {
          return this.content = this.$t('desktopManage.message.operation', { operation: this.$t('desktopManage.operation.unassignUser') }).toString();
        }
        return this.content = this.$t('desktopManage.message.operation', { operation: this.title }).toString();
      }

    },
    submit () {
      this.loading = true;
      let promiseArray: any = [];
      if (Array.isArray(this.data)) {
        (this.data as any).forEach((item: any) => {
          let ids: any = [];
          ids.push(item.id);
          promiseArray.push((this as any).$pandaApi.desktopOperation(item.desktop_pool, {action: this.action, ids: ids}))
        })
      } else {
        let ids: any = [];
        if (this.action === 'UnassignUser') {
          promiseArray.push((this as any).$pandaApi.relieveDesktopAuth((this.data as any).id, {user_id: ''}))
        } else {
          ids.push((this.data as any).id)
          promiseArray.push((this as any).$pandaApi.desktopOperation((this.data as any).desktop_pool, {action: this.action, ids: ids}))
        }
      }
      Promise.all(promiseArray).then(res => {
        this.loading = false;
        this.visible = false;
        this.$emit('success');
        if (this.action === 'UnassignUser') {
          this.$message({
            message: this.$t('desktopManage.unAssignUserSuccess').toString(),
            type: 'success'
          });
          return;
        }
        this.$message({
            message: this.$t('desktopManage.message.operationSuccess', {subject: this.$t('desktopManage.desktop'), operation: this.title}).toString(),
            type: 'success'
        });
        // console.log(this.data);
        if ((this as any).action === "REBOOT" || (this as any).action === "POWERON" || (this as any).action === "RESET" || (this as any).action === "SUSPEND" || (this as any).action === "POWEROFF") {
          if (Array.isArray(this.data)) {
            this.data.forEach((item: any) => {
              (this as any).$gentianApi.postGenTian({platform_desktop_id: item.platform_desktop_id,resource: item.resource_id, resource_type:item.type === 'HorizonView' ? 'st_resource' : 'mt_resource', action: this.action });
            })
          } else {
            (this as any).$gentianApi.postGenTian({platform_desktop_id: (this.data as any).platform_desktop_id,resource: (this.data as any).resource_id, resource_type: (this.data as any).type === 'HorizonView' ? 'st_resource' : 'mt_resource', action: this.action });
          }
        }
      }).catch(() => {
        this.loading = false;
        if (this.action === 'UnassignUser') {
          this.$message({
            message: this.$t('desktopManage.message.operationFail', {subject: this.$t('desktopManage.desktop'), operation: this.$t('desktopManage.operation.unassignUser')}).toString(),
            type: 'warning'
          });
          return;
        }
        this.$message({
          message: this.$t('desktopManage.message.operationFail', {subject: this.$t('desktopManage.desktop'), operation: this.title}).toString(),
          type: 'warning'
        });
      })
    }
  }
})
</script>
